package com.example.yblin.hello24.DataUtils;

import android.content.Context;
import android.util.Log;

import com.example.yblin.hello24.Calculate;
import com.example.yblin.hello24.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class GameUtil {
    public static final int ADD = 10000;
    public static final int SUB = 20000;
    public static final int MUL = 30000;
    public static final int DIV = 40000;
    public static final int LEFT = 50000;
    public static final int RIGHT = 60000;
    public static final int USAGE_FOR_SERVER = 0;
    public static final int USAGE_FOR_SHOW = 1;
    public static final int[] poker = new int[52];

    public static final String TAG = "GameUtil";
    public static GameData[][] numbers;
    public static ArrayList<Integer>[] expression;
    public static int correctNumber;
    public static int costtime = 0;
    public static ArrayList<String>[] answer;

    static {
        poker[0] = R.drawable.num01;
        poker[1] = R.drawable.num02;
        poker[2] = R.drawable.num03;
        poker[3] = R.drawable.num04;
        poker[4] = R.drawable.num05;
        poker[5] = R.drawable.num06;
        poker[6] = R.drawable.num07;
        poker[7] = R.drawable.num08;
        poker[8] = R.drawable.num09;
        poker[9] = R.drawable.num10;
        poker[10] = R.drawable.num11;
        poker[11] = R.drawable.num12;
        poker[12] = R.drawable.num13;
        poker[13] = R.drawable.num14;
        poker[14] = R.drawable.num15;
        poker[15] = R.drawable.num16;
        poker[16] = R.drawable.num17;
        poker[17] = R.drawable.num18;
        poker[18] = R.drawable.num19;
        poker[19] = R.drawable.num20;
        poker[20] = R.drawable.num21;
        poker[21] = R.drawable.num22;
        poker[22] = R.drawable.num23;
        poker[23] = R.drawable.num24;
        poker[24] = R.drawable.num25;
        poker[25] = R.drawable.num26;
        poker[26] = R.drawable.num27;
        poker[27] = R.drawable.num28;
        poker[28] = R.drawable.num29;
        poker[29] = R.drawable.num30;
        poker[30] = R.drawable.num31;
        poker[31] = R.drawable.num32;
        poker[32] = R.drawable.num33;
        poker[33] = R.drawable.num34;
        poker[34] = R.drawable.num35;
        poker[35] = R.drawable.num36;
        poker[36] = R.drawable.num37;
        poker[37] = R.drawable.num38;
        poker[38] = R.drawable.num39;
        poker[39] = R.drawable.num40;
        poker[40] = R.drawable.num41;
        poker[41] = R.drawable.num42;
        poker[42] = R.drawable.num43;
        poker[43] = R.drawable.num44;
        poker[44] = R.drawable.num45;
        poker[45] = R.drawable.num46;
        poker[46] = R.drawable.num47;
        poker[47] = R.drawable.num48;
        poker[48] = R.drawable.num49;
        poker[49] = R.drawable.num50;
        poker[50] = R.drawable.num51;
        poker[51] = R.drawable.num52;
    }

    public static int getNewProblem(Context context) {
        Map<String, String> loginInfo = new HashMap<>();
        loginInfo.put("username", LocalDataUtil.read(context, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.USERNAME));
        loginInfo.put("pwd", LocalDataUtil.read(context, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.PASSWORD));

        NetUtil netUtil = new NetUtil(NetUtil.ADDR_STAGE_START, loginInfo);
        Map<String, String> returnData = netUtil.getData();

        if (returnData.get("code").equals("" + NetUtil.CODE_OK)) {
            for (int i = 0; i < 1; i++) {
                Map<String, String> pair = JsonUtil.analyze(
                        returnData.get("" + (i + 1)));
                numbers[i][0].number = Integer.parseInt(pair.get("a"));
                numbers[i][1].number = Integer.parseInt(pair.get("b"));
                numbers[i][2].number = Integer.parseInt(pair.get("c"));
                numbers[i][3].number = Integer.parseInt(pair.get("d"));
                numbers[i][0].selected = false;
                numbers[i][1].selected = false;
                numbers[i][2].selected = false;
                numbers[i][3].selected = false;
                expression[0].clear();
                Log.i(TAG, i + ":" + numbers[i][0].number + numbers[i][1].number + numbers[i][2].number + numbers[i][3].number);
            }
            return NetUtil.CODE_OK;
        } else {
            return Integer.parseInt(returnData.get("code"));
        }
    }

    public static int stageStart(Context context) {
        numbers = new GameData[1][4];
        numbers[0][0] = new GameData();
        numbers[0][1] = new GameData();
        numbers[0][2] = new GameData();
        numbers[0][3] = new GameData();

        expression = new ArrayList[1];
        expression[0] = new ArrayList<>();
        correctNumber = 0;
        return getNewProblem(context);
    }

    public static int stageEnd(Context context) {
        //网络提交
        Map<String, String> loginInfo = new HashMap<>();
        loginInfo.put("username", LocalDataUtil.read(context, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.USERNAME));
        loginInfo.put("pwd", LocalDataUtil.read(context, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.PASSWORD));
        loginInfo.put("correctNumber", correctNumber + "");

        NetUtil netUtil = new NetUtil(NetUtil.ADDR_STAGE_END, loginInfo);
        Map<String, String> returnData = netUtil.getData();

        if (returnData.get("code").equals("" + NetUtil.CODE_OK)) {
            return NetUtil.CODE_OK;
        } else {
            return Integer.parseInt(returnData.get("code"));
        }
    }

    public static int stageNext(Context context) {
        boolean b = check();
        if (b) {
            correctNumber++;//这边表示闯关数
            return getNewProblem(context);
        } else {
            stageEnd(context);
            return 0;
        }
    }

    public static int start(Context context) {
        numbers = new GameData[11][4];
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 4; j++) {
                numbers[i][j] = new GameData();
            }
        }
        expression = new ArrayList[11];
        for (int i = 0; i < 11; i++) {
            expression[i] = new ArrayList<>();
        }

        Map<String, String> loginInfo = new HashMap<>();
        loginInfo.put("username", LocalDataUtil.read(context, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.USERNAME));
        loginInfo.put("pwd", LocalDataUtil.read(context, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.PASSWORD));

        NetUtil netUtil = new NetUtil(NetUtil.ADDR_START, loginInfo);
        Map<String, String> returnData = netUtil.getData();

        if (returnData.get("code").equals("" + NetUtil.CODE_OK)) {
            for (int i = 1; i <= 10; i++) {
                Map<String, String> pair = JsonUtil.analyze(
                        returnData.get("" + i));
                numbers[i][0].number = Integer.parseInt(pair.get("a"));
                numbers[i][1].number = Integer.parseInt(pair.get("b"));
                numbers[i][2].number = Integer.parseInt(pair.get("c"));
                numbers[i][3].number = Integer.parseInt(pair.get("d"));
                Log.i(TAG, i + ":" + numbers[i][0].number + numbers[i][1].number + numbers[i][2].number + numbers[i][3].number);
            }
            return NetUtil.CODE_OK;
        } else {
            return Integer.parseInt(returnData.get("code"));
        }
    }


    public static boolean check() {
        Boolean b = Calculate.returnAnswer(toString(expression[0], USAGE_FOR_SERVER),
                    numbers[0][0].number,
                    numbers[0][1].number,
                    numbers[0][2].number,
                    numbers[0][3].number);
        return b == null ? false : b;
    }


    public static int end(Context context) {
        correctNumber = 0;
        //表达式检查
        for (int i = 1; i <= 10; i++) {
            Boolean b = Calculate.returnAnswer(toString(expression[i], USAGE_FOR_SERVER),
                    numbers[i][0].number,
                    numbers[i][1].number,
                    numbers[i][2].number,
                    numbers[i][3].number);
            if (expression[i].isEmpty()) b = false;
            if (b == null) {
                return i + 10000;//返回值为表达式+10000
            } else if (b == false) {
                //什么都不做
            } else {
                correctNumber++;
            }
        }


        //网络提交
        Map<String, String> loginInfo = new HashMap<>();
        loginInfo.put("username", LocalDataUtil.read(context, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.USERNAME));
        loginInfo.put("pwd", LocalDataUtil.read(context, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.PASSWORD));
        loginInfo.put("correctNumber", correctNumber + "");
        for (int i = 1; i <= 10; i++) {
            loginInfo.put(
                    i + "",                                //key
                    hash(numbers[i][0].number,
                            numbers[i][1].number,
                            numbers[i][2].number,
                            numbers[i][3].number) + "");      //value
        }
        NetUtil netUtil = new NetUtil(NetUtil.ADDR_END, loginInfo);
        Map<String, String> returnData = netUtil.getData();

        if (returnData.get("code").equals("" + NetUtil.CODE_OK)) {
            costtime = Integer.parseInt(returnData.get("costtime"));
            answer = new ArrayList[11];
            for (int i = 0; i < 11; i++) {
                answer[i] = new ArrayList<>();
            }
            for (Map.Entry<String, String> entry : returnData.entrySet()) {
                if (!entry.getKey().equals("code") && !entry.getKey().equals("costtime")) {
                    Map<String, String> exp = JsonUtil.analyze(entry.getValue());
                    for (Map.Entry<String, String> expEntry : exp.entrySet()) {
                        if (!expEntry.getKey().equals("exp")) {
                            answer[Integer.parseInt(entry.getKey())].add(expEntry.getValue());
                        }
                    }
                }
            }


            return NetUtil.CODE_OK;
        } else {
            return Integer.parseInt(returnData.get("code"));
        }

    }

    public static class GameData {
        public int number;
        public boolean selected;

        public GameData() {
            number = 0;
            selected = false;
        }
    }

    public static String toString(ArrayList<Integer> expList, int usage) {
        StringBuilder builder = new StringBuilder();
        if (usage == USAGE_FOR_SERVER) {
            for (int n : expList) {
                if (n == ADD) {
                    builder.append('+');
                } else if (n == SUB) {
                    builder.append('-');
                } else if (n == MUL) {
                    builder.append('*');
                } else if (n == DIV) {
                    builder.append('/');
                } else if (n == LEFT) {
                    builder.append('(');
                } else if (n == RIGHT) {
                    builder.append(')');
                } else {
                    builder.append(n);
                }
            }
        } else {
            for (int n : expList) {
                if (n == ADD) {
                    builder.append('＋');
                } else if (n == SUB) {
                    builder.append('－');
                } else if (n == MUL) {
                    builder.append('×');
                } else if (n == DIV) {
                    builder.append('÷');
                } else if (n == LEFT) {
                    builder.append('（');
                } else if (n == RIGHT) {
                    builder.append('）');
                } else {
                    builder.append(n);
                }
            }
        }
        return builder.toString();
    }

    private static int hash(int a, int b, int c, int d) {
        int[] num = new int[]{a, b, c, d};
        Arrays.sort(num);
        return num[0] * 1000000
                + num[1] * 10000
                + num[2] * 100
                + num[3];
    }

}